package org.bean;

import com.google.common.collect.Lists;
import com.sojoys.artifact.tools.ToolError;
import org.error.GameErrorCode;

import java.util.List;

/**
 * 副本队伍
 * Created by fxf on 2017/12/4.
 */
public class DungeonTeam {

    /**
     * 队伍最大人数
     */
    public static final int MAX_MEMBER_SIZE = 3;

    private int id;

    /**
     * 队员
     */
    private List<DungeonTeamMember> memberList;

    /**
     * 匹配开关
     */
    private boolean matching;

    /**
     * 关卡cid
     */
    private int levelCid;

    public DungeonTeam(int id, int levelCid, boolean matching) {
        this.id = id;
        this.matching = matching;
        this.levelCid = levelCid;
        memberList = Lists.newArrayList();
    }

    public DungeonTeamMember getMember(int pid) {
        for (DungeonTeamMember member : memberList) {
            if (member.getPid() == pid)
                return member;
        }
        return null;
    }

    /**
     * 获取队长
     */
    public DungeonTeamMember getLeader() {
        return memberList.stream().filter(member -> member.isLeader())
                .findFirst().get();
    }

    public DungeonTeamMember addMember(PlayerInfo playerInfo) {
        synchronized (this) {
            ToolError.isAndTrue(GameErrorCode.TEAM_IS_FULL, "", memberList.size() >= MAX_MEMBER_SIZE);
            DungeonTeamMember newMember = new DungeonTeamMember(playerInfo.getPid(),
                    playerInfo.getLvl(),
                    playerInfo.getFightPower(),
                    playerInfo.getName(),
                    playerInfo.getHelpHeroCid(),
                    playerInfo.getServerId(),
                    false,
                    false);
            memberList.add(newMember);
            return newMember;
        }
    }


    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~getter、setter~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public List<DungeonTeamMember> getMemberList() {
        return memberList;
    }

    public void setMemberList(List<DungeonTeamMember> memberList) {
        this.memberList = memberList;
    }

    public boolean isMatching() {
        return matching;
    }

    public void setMatching(boolean matching) {
        this.matching = matching;
    }

    public int getLevelCid() {
        return levelCid;
    }

    public void setLevelCid(int levelCid) {
        this.levelCid = levelCid;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
